<template>
	<view class="change-identity">
		<view class="change-identity-item-contain">


			<view class="change-identity-item" @click="change('normal')">
				<view class="">
					普通用户权限
				</view>
				<view class="change-identity-item-now" v-if="!currentOpertor">
					当前
				</view>
			</view>
			<view class="change-identity-item" @click="change('identity')">
				<view class="">
					管理员权限
				</view>
				<view class="change-identity-item-now" v-if="currentOpertor">
					当前
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue"
	import {
		onLoad
	} from '@dcloudio/uni-app'
	const currentOpertor = ref(false)
	import {
		useUserStore
	} from '@/store/user'
	const userStore = useUserStore()
	onLoad(() => {
		currentOpertor.value = uni.getStorageSync('identity') || false
	})
	const change = (val) => {
		if (val == "normal") {
			if (!currentOpertor.value) {
				uni.navigateBack();
			} else {
				uni.setStorageSync('identity', false)
				userStore.setItemName(null);
				userStore.setItemId(null);
				currentOpertor.value = false
				uni.$emit('changePage', {
					from: 'changeIdentify'
				}, "normal")
				// 添加延迟确保存储更新完成
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					})

				}, 500)
			}
		}


		if (val == "identity") {
			if (currentOpertor.value) {
				uni.navigateBack();
			} else {
				uni.setStorageSync('identity', true)
				userStore.setItemName(null); // 清除项目名称
				userStore.setItemId(null);
				userStore.updAdminType(true)
				currentOpertor.value = true
				uni.$emit('changePage', {
					from: 'changeIdentify'
				}, "currentOpertor")
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					})

				}, 200)
			}
		}


	}
</script>

<style lang="scss" scoped>
	.change-identity {
		width: 100vw;
		height: 100vh;
		background: #f7f7f7;

		.change-identity-item-contain {
			padding-top: 100rpx;

			.change-identity-item {
				position: relative;
				width: 80vw;
				margin: 0 auto;
				background: #fff;
				border-radius: 10px;
				height: 100rpx;
				line-height: 100rpx;
				margin-bottom: 30rpx;
				padding: 0 30rpx;

				.change-identity-item-now {
					position: absolute;
					top: 0;
					right: 0;
					padding: 10rpx 15rpx;
					line-height: 30rpx;
					border-bottom-left-radius: 10px;
					border-top-right-radius: 10px;
					color: #25afe5;
					background: rgba(90, 201, 241, 0.2)
				}
			}

		}

	}
</style>